﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Threading;

namespace Reporter.Controls
{
    public class ImportInfo : INotifyPropertyChanged
    {
        // three state : false = stage not started, null = stace in progress, true = stage finished
        private bool? _dataInitialized;
        private bool? _notebookInitialized;
        private bool? _dataImported;
        private bool? _imagesImported;
        private string _ellapsedTime = "00:00:00";
        private DateTime _startingTime;
        private DispatcherTimer _timer;

        public string EllapsedTime
        {
            get { return _ellapsedTime; }
            set
            {
                if (this._ellapsedTime != value)
                {
                    this._ellapsedTime = value;
                    this.NotifyPropertyChanged("EllapsedTime");
                }
            }
        }

        public bool? DataInitialized
        {
            get { return this._dataInitialized; }
            set
            {
                if (this._dataInitialized != value)
                {
                    this._dataInitialized = value;
                    this.NotifyPropertyChanged("DataInitialized");
                }
            }
        }

        public bool? NotebookInitialized
        {
            get { return this._notebookInitialized; }
            set
            {
                if (this._notebookInitialized != value)
                {
                    this._notebookInitialized = value;
                    this.NotifyPropertyChanged("NotebookInitialized");
                }
            }
        }

        public bool? DataImported
        {
            get { return this._dataImported; }
            set
            {
                if (this._dataImported != value)
                {
                    this._dataImported = value;
                    this.NotifyPropertyChanged("DataImported");
                }
            }
        }

        public bool? ImagesImported
        {
            get { return this._imagesImported; }
            set
            {
                if (this._imagesImported != value)
                {
                    this._imagesImported = value;
                    this.NotifyPropertyChanged("ImagesImported");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        public void StartTimer()
        {
            _timer = new DispatcherTimer();
            _timer.Tick += new EventHandler(Timer_Tick);
            _timer.Interval = TimeSpan.FromSeconds(1);

            _startingTime = DateTime.Now;
            _timer.Start();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            var time = DateTime.Now - _startingTime;
            var hours = (time.Hours < 10) ? string.Format("0{0}", time.Hours) : time.Hours.ToString();
            var minutes = (time.Minutes < 10) ? string.Format("0{0}", time.Minutes) : time.Minutes.ToString();
            var seconds = (time.Seconds < 10) ? string.Format("0{0}", time.Seconds) : time.Seconds.ToString();
            EllapsedTime = String.Format("{0}:{1}:{2}", hours, minutes, seconds);
        }

        public void StopTimer()
        {
            _timer.Stop();
        }


    }
}
